package com.hanbang.taitaigou.ui.activity.application;

import android.app.Application;
import android.content.IntentFilter;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.os.Environment;

import com.hanbang.taitaigou.BuildConfig;
import com.hanbang.taitaigou.model.UserInforData;
import com.hanbang.taitaigou.ui.activity.base.BaseActivity;
import com.hanbang.taitaigou.utils.db.DbUtil;
import com.hanbang.taitaigou.utils.http.HttpCallBack;
import com.hanbang.taitaigou.utils.http.HttpRequestParams;
import com.hanbang.taitaigou.utils.other.SharedPreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.io.File;

public class MyApplication extends Application {
    public NetWorkType netWorkType = NetWorkType.NULL;
    public Typeface typeface = null;
    public static String appCachePath;
    public static String appFilePath;
    public static String appSDCachePath;

    public static int shangjiaId = -1;

    public enum NetWorkType {
        WIFI, ETHERNET, MOBILE, NULL
    }


    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG);
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        typeface = Typeface.createFromAsset(getAssets(), "fzlt.ttf");
        appCachePath = getCacheDir().getPath();
        appFilePath = getFilesDir().getPath();
        appSDCachePath = Environment.getExternalStorageDirectory().getPath()
                + "/taitaigou/cache";
        File file = new File(appSDCachePath);
        if (file.exists() || file.mkdirs())
            ;

        BaseActivity.userData = getDbUserData();
        String userInfo = BaseActivity.userData == null ? "no_login" : BaseActivity.userData
                .getMobile();
//		// 云测
//		TestinAgentConfig config = new TestinAgentConfig.Builder(this)
//				.withAppKey("c71e9270da81a35a1f6473133c2df9b5") // Appkey of
//				// your
//				// appliation,
//				// required
//				.withAppChannel(null) // Channel of your application
//				.withUserInfo(userInfo) // User infomation like login account of
//										// user
//				.withDebugModel(false) // Output the crash log in local if you
//										// open debug mode
//				.withErrorActivity(true) // Output the activity info in crash or
//											// error log
//				.withCollectNDKCrash(true) // Collect NDK crash or not if you
//											// use our NDK
//				.withOpenCrash(true) // Monitor crash if true
//				.withReportOnlyWifi(false) // Report data only on wifi mode
//				.withReportOnBack(true) // allow to report data when application
//										// in background
//				.build();
//		TestinAgent.init(config);

    }

    private UserInforData getDbUserData() {
        UserInforData userData = null;
        String id = SharedPreferencesUtils.getSharedPreferencesKeyAndValue(this,
                SharedPreferencesUtils.USER_DATA, "user_id");
        if (id != null && !id.equals("-1")) {
            try {
                userData = DbUtil.getDbUtils().findById(
                        UserInforData.class, id);
            } catch (DbException e) {
                e.printStackTrace();
                userData = null;
            }
        } else {
            userData = null;
        }
        return userData;
    }

    /**
     * 获取最近商家
     * http://ttgtext.hbung.com/tools/app_api.ashx?action=RecentMerchant&user_id=9
     */

    public void getZuijin() {
        if (BaseActivity.userData != null) {
            HttpRequestParams params = new HttpRequestParams();
            params.addBodyParameter("action", "RecentMerchant");
            params.addBodyParameter("user_id", String.valueOf(BaseActivity.userData.getUser_id()));
            x.http().post(params, new HttpCallBack<String>(this, null) {

                @Override
                public void onSuccess(String result) {
                    super.onSuccess(result);
                    try {
                        JSONObject json = new JSONObject(result);
                        if (json.getInt("status") == 1) {
                            shangjiaId = json.getInt("result");
                        } else {
                            shangjiaId = -1;
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    public void onTerminate() {
        super.onTerminate();
    }

    ;

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }
}
